#include <sourcemod>

#define PLUGIN_VERSION "1.1.5"

public Plugin:myinfo = 
{
    name = "重连需要等待的时间",
    author = "汉化一夜男",
    description = "Blocking people for time from reconnecting",
    version = PLUGIN_VERSION,
    url = "www.sourcemod.net"
}

new bool:g_bKickedByPlugin[MAXPLAYERS+1];

new Handle:g_kvDB = INVALID_HANDLE;

//CVars' handles
new Handle:cvar_ar_time = INVALID_HANDLE;


//Cvars' varibles
new ar_time = 30;
new bool:g_bInSeverClient[MAXPLAYERS+1];


public OnPluginStart()
{
    g_kvDB = CreateKeyValues("antireconnect");
    
    CreateConVar("sm_anti_reconnect_version", PLUGIN_VERSION, "Anti-Reconnect Version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY|FCVAR_DONTRECORD);
    cvar_ar_time = CreateConVar("sm_anti_reconnect_time", "40", "离开服务器后玩家需要等待多少秒才能再次进入服务器？, 0 = 禁用", FCVAR_PLUGIN, true, 0.0);

    
    HookConVarChange(cvar_ar_time, OnCVarChange);

    //AutoExecConfig(true, "plugin.antireconnect");
    //LoadTranslations("antireconnect.phrases");
}

public OnCVarChange(Handle:convar_hndl, const String:oldValue[], const String:newValue[])
{
    GetCVars();
}


public OnConfigsExecuted()
{
    GetCVars();
}

public OnMapStart()
{
    CloseHandle(g_kvDB);
    g_kvDB = CreateKeyValues("antireconnect");
}

public GetCVars()
{
    ar_time = GetConVarInt(cvar_ar_time);
}

public OnClientDisconnect(client)
{    
    if(!IsFakeClient(client) && g_bInSeverClient[client])
    {
        if (g_bKickedByPlugin[client] || !client)
        return;

        decl String:steamId[30];
        GetClientIP(client, steamId, sizeof(steamId), false);
        
        KvSetNum(g_kvDB, steamId, RoundToFloor(GetTickedTime()));// 防重连
        
        //PrintToChatAll("__%N__%s_退出____-_%d", client, steamId, KvGetNum(g_kvDB, steamId, -1));
        g_bInSeverClient[client] = false;
    }
}
//public bool:OnClientConnect(client)
public OnClientConnected(client)
{
    if(!IsFakeClient(client))
    {
        g_bKickedByPlugin[client] = false;
        g_bInSeverClient[client] = false;
    
        decl String:steamId[30];    
        //GetClientAuthString(client, steamId, sizeof(steamId));
        //GetClientName(client, steamId, sizeof(steamId));
        GetClientIP(client, steamId, sizeof(steamId), false);
    
        new disconnect_time = KvGetNum(g_kvDB, steamId, -1);
        //PrintToChatAll("__%N__%s__连接中__   %d", client, steamId, disconnect_time);
        if (disconnect_time == -1)
        {
            return true;
        }
        new wait_time = disconnect_time + ar_time - RoundToFloor(GetTickedTime());
        //PrintToChatAll("__%s__等待时间__%d", steamId, wait_time);
        if (wait_time <= 0)
        {
            KvDeleteKey(g_kvDB, steamId);
            return true;
        }
        else
        {
            g_bKickedByPlugin[client] = true;
            //KickClient(client, "%t", "You are not allowed to reconnect for X seconds", wait_time);
            KickClient(client, "骚年，别撸的太快，请在 %d 秒后再次尝试", wait_time);
            //LogAction(-1, client,"Kicked \"%L\". Player is not allowed to reconnect for %d seconds.", client, wait_time);
            return false;
        }        
    }    
    else
    {
        return true;
    }
}
//玩家连接
public OnClientPutInServer(client)
{
    if(!IsFakeClient(client))
    {
        g_bInSeverClient[client] = true;
    }
}
